package com.example.demo.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.CharacterPickerDialog;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson2.JSONObject;

import androidx.appcompat.app.AppCompatActivity;
import com.example.demo.databinding.ActivityAstroBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AstroActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "AstroActivity";

    private ActivityAstroBinding binding;

    private String url = "https://api.jisuapi.com/astro/fortune?astroid=@astroid@&date=@date@&appkey=d65c8d176fe7c273";

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private String astroName = "白羊座";
    private String astroId = "1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityAstroBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.astroSelect.setText(astroName);

        binding.astroSelect.setOnClickListener(this);
        binding.astroSearch.setOnClickListener(this);

        setJump();
    }

    private void setJump(){
        binding.jumpToAstro.setOnClickListener(view -> {
            startActivity(new Intent(this, AstroActivity.class));
        });
        binding.jumpToHis.setOnClickListener(view -> {
            startActivity(new Intent(this, DayInHistoryActivity.class));
        });
        binding.jumpToGold.setOnClickListener(view -> {
            startActivity(new Intent(this, GoldActivity.class));
        });
        binding.jumpToNews.setOnClickListener(view -> {
            startActivity(new Intent(this, NewsListActivity.class));
        });
        binding.jumpToPhone.setOnClickListener(view -> {
            startActivity(new Intent(this, PhoneActivity.class));
        });
    }


    private void loadInfo() {
        new Thread(() -> {
            try{
                OkHttpClient client = new OkHttpClient();
                String url = this.url.replace("@astroid@", this.astroId)
                        .replace("@date@", dateFormat.format(new Date()));
                Request request = new Request.Builder() .url(url) .build();
                Response response = client.newCall(request).execute();
                String body = response.body().string();

                AstroActivity.this.runOnUiThread(() -> {
                    Log.i(TAG, body);
                    JSONObject mapObj = JSONObject.parseObject(body);
                    JSONObject resultObj = mapObj.getJSONObject("result");
                    JSONObject weekObj = resultObj.getJSONObject("week");

                    binding.astroDate.setText(weekObj.getString("date"));
                    binding.astroMoney.setText(weekObj.getString("money"));
                    binding.astroHealth.setText(weekObj.getString("health"));
                    binding.astroLove.setText(weekObj.getString("love"));
                    binding.astroCareer.setText(weekObj.getString("career"));

                });

            }catch (Exception e){
                Log.i(TAG, Objects.requireNonNull(e.getMessage()));
            }
        }).start();

    }

    @Override
    public void onClick(View view) {
        if(view == binding.astroSelect){
            final String[] items = {
                    "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座",
                    "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座"};
            AlertDialog.Builder dialog = new AlertDialog.Builder(AstroActivity.this);
            dialog.setTitle("我是标题").setItems(items, (dialog1, which) -> {
                this.astroId = (which+1)+"";
                this.astroName = items[which];

                AstroActivity.this.runOnUiThread(() -> binding.astroSelect.setText(AstroActivity.this.astroName));
            });
            dialog.show();
        } else if (view == binding.astroSearch){
            loadInfo();
        }
    }
}